import storage from 'good-storage'
import { USER_KEY } from '@/assets/js/constant'

function get(api, data) {
  return new Promise((resolve, reject) => {
    if (api === 'getChatMsgList') {
      const msg = []
      const userId = storage.session.get(USER_KEY).userId
      for (let i = 0; i < 20; i++) {
        msg.push({
          id: i + 1,
          userId: i % 3 === 0 ? userId : 2,
          content: `${data.groupId}信息${i}`
        })
      }
      resolve(msg)
    } else if (api === 'getUserList') {
      const groups = []
      const words = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
      for (let i = 0; i < words.length; i++) {
        const word = words[i]
        const group = { title: word, list: [] }
        for (let j = 0; j < 4; j++) {
          group.list.push({
            userId: j + 1,
            name: `${word}对话的用户${j + 1}`,
            age: 40 + j,
            date: '2021-02-04',
            avatar: ''
          })
        }
        groups.push(group)
      }
      resolve(groups)
    } else if (api === 'getChatList') {
      const chatList = []
      for (let i = 0; i < 20; i++) {
        chatList.push({
          groupId: `${i}`,
          name: `用户${i}`,
          message: `用户${i}，你好！`
        })
      }
      resolve(chatList)
    }
  })
}

export const getChatMsgList = function(data) {
  return get('getChatMsgList', data)
}

export const getUserList = function(data) {
  return get('getUserList', data)
}

export const getChatList = function(data) {
  return get('getChatList', data)
}
